package com.notary.controller;

import cn.hutool.core.util.IdUtil;
import com.alibaba.fastjson.JSONObject;
import com.notary.entity.po.GzcRegisterDTO;
import com.notary.entity.vo.JsonResult;
import com.notary.service.SysPlatformOpeningRecordService;
import com.notary.util.RedisUtils;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;

@RestController
@RequestMapping("/platformOpening")
@Valid
public class SysPlatformOpeningController{

    @Resource
    SysPlatformOpeningRecordService sysPlatformOpeningRecordService;

    @Resource
    RedisUtils redisUtils;


    @PostMapping("/getToken/{id}")
    public JsonResult getToken(@PathVariable(value = "id", required = false) String id, @RequestBody JSONObject jsonObject) {

        String token = jsonObject.getString("token"), uuid = IdUtil.simpleUUID(), result;

        if ("set".equals(id)) {
            redisUtils.set("OPEN_PLATFORM_TOKEN_BY_TEMP_" + uuid, token, 7200);
            result = uuid;
            return JsonResult.success("生成成功!", result);
        } else if (id.startsWith("&")) {
            id = id.replaceAll("&", "");
            redisUtils.set("OPEN_PLATFORM_TOKEN_BY_TEMP_" + id, token, 7200);
            return JsonResult.success("修改成功!", id);
        } else {
            result = redisUtils.get("OPEN_PLATFORM_TOKEN_BY_TEMP_" + id);
            return JsonResult.success("获取成功!", result);
        }

    }

    @PostMapping("/updateInfo")
    public JsonResult updateInfo(@RequestBody JSONObject jsonObject) throws Exception {
        JsonResult result = sysPlatformOpeningRecordService.createAdmin(jsonObject.getString("mobile"), jsonObject.getString("pass"), jsonObject.getString("email"));
        if (result.getCode().equals("200")) {
            result = sysPlatformOpeningRecordService.perfectInfo(jsonObject.getString("mobile"), jsonObject.getString("name"), jsonObject.getString("card"));;
        }
        return result;
    }

    @PostMapping("/savePlatform")
    public JsonResult savePlatformInfo(@RequestBody JSONObject jsonObject) {
        return sysPlatformOpeningRecordService.savePlatform(jsonObject);
    }


    @GetMapping("/updateStatus")
    public void updateStatus() {
        sysPlatformOpeningRecordService.updateConfigStatus();
    }


    @GetMapping("/getConfig")
    public JsonResult getConfig() {
        return sysPlatformOpeningRecordService.getConfig();
    }

    @PostMapping("/register")
    public JsonResult register(@Valid @RequestBody GzcRegisterDTO gzcRegisterDTO){
        return sysPlatformOpeningRecordService.register(gzcRegisterDTO);
    }
    //认证接口
    @GetMapping ("/attestation")
    public JsonResult attestation(){
        return sysPlatformOpeningRecordService.attestation();

    }

    //签完合同回调，修改sysconfig
    @GetMapping("/updateConfig")
    public JsonResult updateConfig( @ApiParam(value="appid")
                                    @NotBlank(message = "appid不能为空")
                                    @RequestParam String appid,
                                    @ApiParam(value = "密钥")
                                    @NotBlank(message = "secret不能为空")
                                    @RequestParam String secret){
        return sysPlatformOpeningRecordService.updateConfig(appid,secret);
    }
}
